<template>
  <div v-if="model">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>英雄管理</el-breadcrumb-item>
      <el-breadcrumb-item>{{id?'编辑':'创建'}}英雄</el-breadcrumb-item>
    </el-breadcrumb>
    <el-form label-position="right" label-width="100px">
      <el-tabs v-model="activeName" type="border-card" style="margin-top:15px">
        <el-tab-pane label="基本信息" name="basicInfo">
          <el-form-item label="英雄名称">
            <el-input style="width:250px" v-model="model.name"></el-input>
          </el-form-item>
          <el-form-item label="英雄头像">
            <el-upload
              class="avatar-uploader"
              :action="$http.defaults.baseURL +'/upload'"
              :show-file-list="false"
              :headers="getAuthHeaders()"
              :on-success="res=>$set(model,'avatar',res.url)"
            >
              <img v-if="model.avatar" :src="model.avatar"  style="width:100%; height: 100%;" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
            <el-form-item label="Banner">
              <el-upload
                      class="avatar-uploader"
                      :action="$http.defaults.baseURL +'/upload'"
                      :show-file-list="false"
                      :headers="getAuthHeaders()"
                      :on-success="res=>$set(model,'banner',res.url)"
              >
                <img v-if="model.banner" :src="model.banner" class="avatar"/>
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          <el-form-item label="英雄称号">
            <el-input style="width:250px" v-model="model.title"></el-input>
          </el-form-item>
          <el-form-item label="所属分类">
            <el-select v-model="model.categories" placeholder multiple>
              <el-option
                v-for="item in optionData"
                :key="item._id"
                :label="item.name"
                :value="item._id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="难度">
            <el-rate
              v-model="model.scores.difficult"
              :colors="colors"
              :max="10"
              :low-threshold="3"
              :high-threshold="7"
              show-score
            ></el-rate>
          </el-form-item>
          <el-form-item label="技能">
            <el-rate
              v-model="model.scores.skills"
              :colors="colors"
              :max="10"
              :low-threshold="3"
              :high-threshold="7"
              show-score
            ></el-rate>
          </el-form-item>
          <el-form-item label="攻击">
            <el-rate
              v-model="model.scores.attack"
              :colors="colors"
              :max="10"
              show-score
              :low-threshold="3"
              :high-threshold="7"
            ></el-rate>
          </el-form-item>
          <el-form-item label="存活">
            <el-rate
              v-model="model.scores.survive"
              :colors="colors"
              :max="10"
              :low-threshold="3"
              :high-threshold="7"
              show-score
            ></el-rate>
          </el-form-item>
          <el-form-item label="顺风出装">
            <el-select v-model="model.items1" placeholder multiple>
              <el-option
                v-for="item in equipData"
                :key="item._id"
                :label="item.name"
                :value="item._id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="逆风出装">
            <el-select v-model="model.items2" placeholder multiple>
              <el-option
                v-for="item in equipData"
                :key="item._id"
                :label="item.name"
                :value="item._id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="使用技巧">
            <el-input style="width:50%;height:auto" v-model="model.usageTips" type="textarea"></el-input>
          </el-form-item>
          <el-form-item label="对抗技巧">
            <el-input style="width:50%;height:auto" v-model="model.battleTips" type="textarea"></el-input>
          </el-form-item>
          <el-form-item label="团战思路">
            <el-input style="width:50%;height:auto" v-model="model.teamTips" type="textarea"></el-input>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="技能" name="skill">
          <el-button type="primary" @click="model.skills.push({})">添加技能</el-button>
          <el-row type="flex" style=" flex-wrap: wrap;  ">
            <el-col v-for="(item,i) in model.skills" :key="i" :span="12" class="col">
              <el-form-item label="技能名称">
                <el-input v-model="item.name" style="width:22rem;"></el-input>
              </el-form-item>

              <el-form-item label="技能图片">
                <el-upload
                  class="avatar-uploader"
                  :action="$http.defaults.baseURL +'/upload'"
                  :show-file-list="false"
                  :headers="getAuthHeaders()"
                  :on-success="res=>$set(item,'icon',res.url)"
                >
                  <img v-if="item.icon" :src="item.icon" class="avatar" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              <el-form-item label="冷却值">
                <el-input v-model="item.delay" style="width:22rem;"></el-input>
              </el-form-item>
              <el-form-item label="消耗">
                <el-input v-model="item.cost" style="width:22rem;"></el-input>
              </el-form-item>
              <el-form-item label="技能介绍">
                <el-input
                  v-model="item.description"
                  style="width:22rem;height:auto"
                  type="textarea"
                ></el-input>
              </el-form-item>
              <el-form-item label="小提示">
                <el-input v-model="item.tips" style="width:22rem;height:auto" type="textarea"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button size="small" type="danger" @click="model.skills.splice(i,1)">删除</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="召唤师技能" name="heroSkill">
          <el-button type="primary" @click="model.heroSkills.push({})">添加技能</el-button>
          <el-row type="flex" style="flex-wrap: wrap;  ">
            <el-col v-for="(item,i) in model.heroSkills" :key="i" :span="12" class="col">
              <el-form-item label="技能名称">
                <el-input v-model="item.name" style="width:22rem;"></el-input>
              </el-form-item>
              <el-form-item label="图片">
                <el-upload
                        class="avatar-uploader"
                        :action="$http.defaults.baseURL +'/upload'"
                        :show-file-list="false"
                        :headers="getAuthHeaders()"
                        :on-success="res=>$set(item,'icon',res.url)">
                  <img v-if="item.icon" :src="item.icon" class="avatar" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              <el-form-item>
                <el-button size="small" type="danger" @click="model.heroSkills.splice(i,1)">删除</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="铭文推荐" name="recommend">
          <el-button type="primary" @click="model.recommend.push({})">添加铭文</el-button>
          <el-row type="flex" style="flex-wrap: wrap;  ">
            <el-col v-for="(item,i) in model.recommend" :key="i" :span="12" class="col">
              <el-form-item label="铭文名称">
                <el-input v-model="item.name" style="width:22rem;"></el-input>
              </el-form-item>
              <el-form-item label="图片">
                <el-upload
                        class="avatar-uploader"
                        :action="$http.defaults.baseURL +'/upload'"
                        :show-file-list="false"
                        :headers="getAuthHeaders()"
                        :on-success="res=>$set(item,'icon',res.url)">
                  <img v-if="item.icon" :src="item.icon" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              <el-form-item label="铭文描述1">
                <el-input v-model="item.description1" style="width:20rem;"></el-input>
              </el-form-item>
              <el-form-item label="铭文描述2">
                <el-input v-model="item.description2" style="width:20rem;"></el-input>
              </el-form-item>
              <el-form-item label="铭文描述3">
                <el-input v-model="item.description3" style="width:20rem;"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button size="small" type="danger" @click="model.recommend.splice(i,1)">删除</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="最佳搭档" name="partners">
        <el-button type="primary" @click="model.partners.push({})">添加英雄</el-button>
        <el-row type="flex" style=" flex-wrap: wrap;  ">
          <el-col v-for="(item,i) in model.partners" :key="i" :span="12" class="col">
            <el-form-item label="英雄">
              <el-select v-model="item.hero" style="width:22rem;" filterable>
                <el-option v-for="hero in heroOption" :key="hero._id" :value="hero._id" :label="hero.name">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="描述">
              <el-input v-model="item.description" style="width:22rem;height:auto" type="textarea"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button size="small" type="danger" @click="model.partners.splice(i,1)">删除</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-tab-pane>
      </el-tabs>
      <el-form-item>
        <el-button type="warning" @click="saveCreate">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'EquipCreate',
  props: ['id'],
  data() {
    return {
      model: {
        partners:[],
        scores: {
          difficult: 0,
          skill: 0,
          attack: 0,
          survive: 0
        },
        skills: [] ,
        heroSkills:[],
        recommend:[]
      },
      activeName: 'basicInfo',
      // 下拉分类菜单数据
      optionData: {},
      // 下拉装备数据
      equipData: {},
      //下拉英雄
      heroOption:[],
      colors: ['#99A9BF', '#FFD700', '#FF7F00']
    }
  },
  methods: {
    // 获取下拉分类菜单数据
    async getOptionData() {
      const res = await this.$http.get(`rest/categories`)
      // this.optionData = res.data
      // // console.log(res.data)
      this.optionData = res.data.filter(item => {
        if (!item.categories) {
          return false
        } else if (item.categories.name === '英雄分类') {
          return true
        }
      })
      console.log(this.optionData)
    },
    // 获取下拉装备数据
    async getEquipData() {
      const res = await this.$http.get(`rest/equips`)
      this.equipData = res.data
    },
    //获取下拉英雄
    async getHeroData() {
      const res = await this.$http.get(`rest/heroes`)
      this.heroOption = res.data
      console.log(this.optionData)
    },
    // 获取图片上传成功后的URL
    // afterUpload(res) {
    //   this.$set(this.model, 'image', res.url)
    // },
    // 创建英雄
    async saveCreate() {
      if (this.id) {
        await this.$http.put(`rest/heroes/${this.id}`, this.model)
        this.$message.success(`成功更新英雄:${this.model.name}`)
        this.$router.push(`/heroes/list`)
      } else {
        await this.$http.post(`rest/heroes`, this.model)
        this.$message.success('成功创建英雄:${this.model.name}')
        this.$router.push(`/heroes/list`)
      }
    },
    //根据ID获取数据
    async getEditData() {
      const res = await this.$http.get(`/rest/heroes/${this.id}`)
      // console.log(this.id)

      this.model = Object.assign(this.model,res.data)
    }
  },
  created() {
    this.getHeroData()
    this.id && this.getEditData()
    this.getOptionData()
    this.getEquipData()
  }
}
</script>
<style>
.  .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.el-icon-plus:before {
  line-height: 100px;
}
.el-rate {
  margin-top: 10px;
}
.col {
  border: 1px solid rgba(100, 100, 100, 0.3);
  margin-top: 15px;
  padding-top: 15px;
}
</style>